package cn.kgc.controller;

import cn.kgc.bean.pojo.CPost;
import cn.kgc.common.Constants;
import cn.kgc.common.Result;
import cn.kgc.service.PostManagerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class PostManagerController {

    @Autowired
    private PostManagerService pms;

    //查
    @RequestMapping("/selectpost")
    public Result selectPost(@RequestParam Integer pageNum, @RequestParam Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<CPost> list = pms.selectAll();
        PageInfo<CPost> info = new PageInfo<>(list);
        return Result.success(Constants.CODE_200,"",info);
    }

    //删
    @RequestMapping(value = "/deletepost/{id}" ,method = RequestMethod.POST)
    public Result deletePost(@PathVariable long id){
        boolean flag = pms.deletePostById(id);
        if (flag == true){
            return Result.success(flag);
        }else{
            return Result.error();
        }
    }

    //增
    @RequestMapping(value = "/addpost" ,method = RequestMethod.POST)
    public Result addPost(@RequestBody CPost cPost){
        boolean flag = pms.addPost(cPost);
        if (flag == true){
            return Result.success(flag);
        }else{
            return Result.error();
        }
    }

    //改
    @RequestMapping(value = "/updatepost",method = RequestMethod.POST)
    public Result updatePost(@RequestBody CPost cPost){
        boolean flag = pms.updatePost(cPost);
        if (flag == true){
            return Result.success(flag);
        }else{
            return Result.error();
        }
    }

    //模糊查询
    @RequestMapping("/selectPostByName")
    public Result selectByName(@RequestParam Integer pageNum,@RequestParam Integer pageSize,@RequestParam String name){
        PageHelper.startPage(pageNum,pageSize);
        List<CPost> list = pms.selectByName(name);
        PageInfo<CPost> info = new PageInfo<>(list);
        return Result.success(Constants.CODE_200,"",info);
    }
}
